﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;
using SanctusSoftware.CcliSongSelectLib.Wpf;
using System.Windows.Forms.Integration;
using SanctusSoftware.CcliSongSelectLib;
using Microsoft.Office.Interop.PowerPoint;
using SanctusSoftware.CcliSongSelectImporter;
using SanctusSoftware.CcliSongSelectLib.Management;
using System.Windows;

namespace CCLI_SongSelect_Importer_Add_in_for_PowerPoint
{
    public partial class ThisAddIn
    {

        private readonly Settings _Settings = new Settings();
        private readonly SongSelectDocumentLibraryManager _Manager = new SongSelectDocumentLibraryManager();


        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            try
            {
                _Manager.Add(@"C:\Files\Sanctus Software\Projects\CCLI SongSelect Library USR Files", true);

                Globals.Ribbons.SongSelectRibbon._InsertSongButton.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(_InsertSongButton_Click);
                Globals.Ribbons.SongSelectRibbon._SettingsButton.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(_SettingsButton_Click);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Starting CCLI SongSelect Importer Add-in: " + ex.Message);
            }
        }

       
        
        void _InsertSongButton_Click(object sender, Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs e)
        {
            try
            {
                if (Application.ActivePresentation == null)
                {
                    MessageBox.Show("No presentation selected");
                }
                else
                {
                    SongSelectSearchWindow w = new SongSelectSearchWindow();
                    
                    ElementHost.EnableModelessKeyboardInterop(w);
                    w.Documents = _Manager.Documents.ToArray();
                    w.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                    if (w.ShowDialog() == true)
                    {
                        foreach (SongSelectDocument doc in w.SelectedDocuments)
                        {
                            InsertDocument(doc);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }

        }

        private void InsertDocument(SongSelectDocument doc)
        {
            try
            {
                if (Application.ActivePresentation != null)
                {
                    foreach (SongPart songPart in doc.SongParts)
                    {
                        int index = Application.ActivePresentation.Slides.Count + 1;
                        CustomLayout layout = Application.ActivePresentation.SlideMaster.CustomLayouts[PpSlideLayout.ppLayoutText];
                        Slide slide = Application.ActivePresentation.Slides.AddSlide(index, layout);


                        string title = doc.Title;
                        if (!string.IsNullOrEmpty(songPart.Name))
                        {
                            title += " - " + songPart.Name;
                        }
                        slide.Shapes[1].TextFrame2.TextRange.Text = title;
                        slide.Shapes[2].TextFrame2.TextRange.Text = songPart.Words;
                        slide.Shapes[2].TextFrame2.TextRange.ParagraphFormat.Bullet.Type = Microsoft.Office.Core.MsoBulletType.msoBulletNone;


                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        void _SettingsButton_Click(object sender, Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs e)
        {
            try
            {
                Settings s = new Settings();
                SettingsWindow sw = new SettingsWindow();
                sw.Settings = s;
                ElementHost.EnableModelessKeyboardInterop(sw);
                if (sw.ShowDialog() == true)
                {

                }
                MessageBox.Show(SettingsRepository.GetXaml(s));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}
